define(window.backboneCommon.getViewDefine("crowd.similarInfo"), function ($,require,Collection,tpl) {
    var defaultsOptions= {
        isInitList: true,
        addCustomCallback: null,
        submitCallback: null
    };
    var pagePrivate = {
        getDefaults :function () {
            return defaultsOptions
        },
        getOptions : function (options) {
            options=$.ExtCommon.isCheckEmpty(options)?{}:options;
            return $.ExtCommon.initMergeJson(this.getDefaults(),options);
        }
    };
    var backboneView= Backbone.View.extend({
        events: {
            "click #btn_addCustom": "addCustom",
            "click #tb_modal_custom .modal-custom-tr": "clickCustomTr",
            "click #btn_customList_refresh": "refreshCustom",
            "click #btn_submitSimilar": "saveSubmit"
        },
        initialize: function () {
            this.listenTo(this.model, "bind:CustomList", this.bindCustomList);
            this.listenTo(this.model, "bind:SimilarInfo", this.bindInfo);
            this.listenTo(this.model, "save:SimilarInfo", this.saveInfoCallback);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(options){
            this.elementModel= {
                modal: $("#modal_detailsSimilar"),
                form: $("#form_detailsSimilar"),
                table: $("#tb_modal_custom"),
                refresh: $("#btn_customList_refresh")
            };
            this.extObject={};
            this.pageData={initPage:false};
            this.pageOptions=$.ExtCommon.initMergeJson(pagePrivate.getOptions(),options);
            this.elementModel.form.ExtParsley();
        },
        initPage:function (id,options) {
            this.pageOptions= $.ExtCommon.initMergeJson(this.pageOptions,options);
            this.collection.getSimilarInfo(id);
        },
        bindInfo:function () {
            var self=this;
            var result=this.model.get("dataSimilarInfo");
            self.pageData.isAdd=result.isAdd;
            self.elementModel.modal.find(".modal-title").text($.ExtCommon.getStringConnect("{0}相似人群",self.pageData.isAdd?"新建":"编辑"));
            self.extObject.formValue=self.elementModel.form.ExtFormValue({isRunBind:false,
                initCallback:function(dataJson){
                    self.pageData.similarInfo=dataJson;
                    $("#txt_num").ExtNumeric("refresh");
                    self._initPageElementShow();
                    self.elementModel.form.ExtParsley("reset");
                    self._showModal();
                }
            });
            self.extObject.formValue.cleanValue();
            self.extObject.formValue.bindValueData(result.res.data);
        },
        _initPageElementShow:function () {
            if(this.pageData.isAdd){
                if(this.pageOptions.isInitList){
                    this.pageOptions.isInitList=false;
                    this._getCustomList();
                }else{
                    this._bindCustomData(this.pageData.customList);
                }
            }else{
                var arr=[];
                arr.push(this.pageData.similarInfo.customInfo);
                this._bindCustomData(arr);
            }
            $("#sp_customList_title").text(this.pageData.isAdd?"可用种子人群列表":"已选种子人群");
            $("#txt_num").prop("disabled", !this.pageData.isAdd);
            $("#btn_addCustom,#btn_customList_refresh").toggle(this.pageData.isAdd);
        },
        _getCustomList:function () {
            this.collection.getCustomList();
        },
        bindCustomList:function () {
            var res=this.model.get("dataCustomList");
            this._bindCustomData(res.data);
            if(this.pageData.isRefresh){
                this.pageData.isRefresh=false;
                $.ExtNotify.success("刷新完成!");
                this.elementModel.refresh.removeClass("fa-spin");
            }
        },
        _bindCustomData:function (list) {
            var html=list.length>0?$("#template_custom_tr").render(list):'<tr><td colspan="3" class="dataTables_empty">没有匹配结果</td></tr>';
            this.elementModel.table.find("tbody").html(html);
            $.ExtNumeric.initAll();
            if(this.pageData.isAdd){this.pageData.customList=list;}
        },
        clickCustomTr:function (e) {
            if(this.pageData.isAdd){
                var el=$(e.currentTarget);
                this.elementModel.table.find(".active").removeClass("active");
                if(el.length>0){el.addClass("active")}
            }
        },
        addCustom:function () {
            var self=this;
            self._hideModal();
            setTimeout(function () {
                if(self.pageOptions.addCustomCallback){self.pageOptions.addCustomCallback()}
            },400);
        },
        refreshCustom:function () {
            var self=this;
            $.ExtNotify.destroy();
            self.pageData.isRefresh=true;
            self.elementModel.refresh.addClass("fa-spin");
            self._getCustomList();
        },
        _checkForm:function (customId) {
            $.ExtNotify.destroy();
            var isCheck=true;
            if(this.pageData.isAdd){
                if($.ExtCommon.isCheckEmpty(customId)) {
                    isCheck=false;
                    $.ExtNotify.error("选择种子来源");
                }
            }
            return this.elementModel.form.ExtParsley("validate") && isCheck;
        },
        saveSubmit:function () {
            var customId=this.pageData.isAdd
                ?this.elementModel.table.find(".active:first").data("id")
                :"";
            if(this._checkForm(customId)){
                var ajaxData = this.elementModel.form.ExtFormValue("getValues");
                ajaxData.custom=customId;
                var infoOriginal = this.elementModel.form.ExtFormValue("getOriginalData");
                if(infoOriginal.id){ajaxData.id=infoOriginal.id}
                this.collection.saveSimilarInfo(ajaxData);
                this._hideModal();
            }
        },
        saveInfoCallback:function () {
            var result=this.model.get("dataSaveSimilar");
            if (result.res.success) {
                $.ExtNotify.success("保存成功!");
                if(this.pageOptions.submitCallback){this.pageOptions.submitCallback(result.isAdd,result.req);}
            }else{
                this._showModal();
                $.ExtNotify.error(result.res.msg);
            }
        },
        _showModal:function(){
            this.elementModel.modal.modal("show");
            $.ExtAssist.modal.computeTop(this.elementModel.modal);
        },
        _hideModal: function () {
            this.elementModel.modal.modal("hide");
        }
    });
    var viewModel=function(options){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el:options.pElement,collection:this.collection,model:this.model});
        this.view.initMain(options);
    };
    viewModel.prototype={
        init:function (id,options) {
            options = options || {};
            this.view.initPage(id, options);
        }
    };
    return viewModel;
});